<?php
class User_model extends Model {
	function __construct() {
		parent::Model();
	}
	
	function add($data){
		$this->db->insert('users',$data);
		return $this->db->insert_id();
	}
	function get($type) {
		$this->db->where('level',$type);
		$query = $this->db->get('users');
		return $query->result_array();
	}
	
	function check_password($data2)
	{
		$sql="select users.customer_id, users.id, users.activated, users.level, users.password from users where users.password ='". $data2['password']."' and users.customer_id=".$data2['customer_id'];	
		//echo $sql;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function update_password_customer($data,$id)
	{
		$this->db->where('customer_id',$id);
		return $this->db->update('users',$data);
	}
	function validate_frontend_new($data) {
		//$this->db->where('username',$data['username']);
		//$this->db->where('password',md5($data['password']));
		//$this->db->where('storeno',$data['storeno']);
		//$this->db->where('level >','0');
		//$query = $this->db->get('users');
		//$query = $this->db->get('customers');
		//if ($query->num_rows() > 0){ 
			//return $query->first_row('array');
		//}
		$storeno = $data['storeno'];
		$sql="select users.customer_id, users.id, users.activated, users.level, users.password from users, customers where customers.storeno = $storeno and users.customer_id=customers.id"	;	
		$query = $this->db->query($sql);
		return $query->first_row('array');
		//return false;
	}
	
	
	function validate_frontend($data) {
		//$this->db->where('username',$data['username']);
		//$this->db->where('password',md5($data['password']));
		//$this->db->where('storeno',$data['storeno']);
		//$this->db->where('level >','0');
		//$query = $this->db->get('users');
		//$query = $this->db->get('customers');
		//if ($query->num_rows() > 0){ 
			//return $query->first_row('array');
		//}
		$storeno = $data['storeno'];
		$sql="select users.customer_id, users.id, users.activated, users.level from users, customers where customers.storeno = $storeno and users.customer_id=customers.id"	;	
		$query = $this->db->query($sql);
		return $query->first_row('array');
		//return false;
	}
	function validate($data) {
		$this->db->where('username',$data['username']);
		$this->db->where('password',md5($data['password']));
		$this->db->where('level >','0');
		$query = $this->db->get('users');
		
		if ($query->num_rows() > 0){ 
			return $query->first_row('array');
		}
		
		return false;
	}
	function get_user_by_cid($id) {
		$this->db->where('customer_id',$id);
		$query = $this->db->get('users');
		return $query->first_row('array');
	}
	function id($id) {
		$this->db->where('id',$id);
		$query = $this->db->get('users');
		return $query->first_row('array');
	}
	function delete($id) {
		$this->db->where('id',$id);
		$this->db->delete('users');
	}
	function update($id,$data) {
		$this->db->where('id',$id);
		$this->db->update('users',$data);
	}
	function recognize($username) {
		$this->db->where('username',$username);
		$query = $this->db->get('users');
		return $query->first_row('array');
	}
	function recognize_v2($name, $type) {
		$sql="select users.customer_id, users.id, users.activated, users.banned, users.username, users.level from users, customers where (customers.storename like '%$name%' or customers.tradingname like '%$name%' or customers.franchisee like '%$name%') and users.level =$type and users.customer_id=customers.id"	;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function recognize_v3($store_number, $type) {
		$sql="select users.customer_id, users.id, users.activated, users.banned, users.username, users.level from users, customers where customers.storeno='".$store_number."' and users.level =$type and users.customer_id=customers.id"	;
		$query = $this->db->query($sql);
		//echo $sql;
		return $query->result_array();
	}
	function recognize_v4($store_number,$type,$name) {
		$sql="select users.customer_id, users.id, users.activated, users.banned, users.username, users.level from users, customers where (customers.storename like '%$name%' or customers.tradingname like '%$name%' or customers.franchisee like '%$name%') and customers.storeno='".$store_number."' and users.level =$type and users.customer_id=customers.id"	;
		$query = $this->db->query($sql);
		//echo $sql;
		return $query->result_array();
	}
	function customer($customer_id) {
		$this->db->where('customer_id',$customer_id);
		$query = $this->db->get('users');
		return $query->first_row('array');
	}
}
?>